from flask import Flask,request,jsonify
import json
import faceless
from multiprocessing import Process
# import subprocess
app = Flask(__name__)

@app.route('/', defaults={'name':"Guest"})
# @app.route('/<string:name>' , methods=['GET'])
def say_hello(name):
    return "Hello " + name

@app.route('/login', methods=['GET', 'POST'])
def login():
    projectId = request.args.get('projectId', '')
    memberId = request.args.get('memberId', '')
    key = request.args.get('key', '')


    config = {
        'projectId': projectId,
        'memberId': memberId,
    }

    # config = {
    #     'projectId': 197,
    #     'memberId': 3622,
    #     'key': 15256758477683301
    # }

    robot = faceless.MyWXBot()
    robot.set_login_config(config)
    robot.get_uuid()
    robot.conf['qr'] = 'tty'
    qrcode = 'https://login.weixin.qq.com/l/'+robot.uuid
    p = Process(target=process_run, args=(robot,))
    p.start()
    resp = jsonify({'status':1,'qrcode':qrcode})
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp
    # robot.DEBUG = True
    # robot.conf['qr'] = 'tty'
    # robot.run()

def process_run(robot):
    robot.run()

if __name__ == "__main__":
    app.run(
        host = '127.0.0.1',
        port = 5002,
        debug=True
    )
    # login()